/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.rmi.server;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;

import edu.ucla.cs.typecast.net.EndPoint;
import edu.ucla.cs.typecast.util.TypeUtil;

/**
 * This class contains a TypeCast service object and its exported interfaces. 
 * @date Jul 25, 2007
 *
 */
public class ServiceRegistrationEntry {
	private EndPoint endPoint; 
	private Class[] types; 
	private Object service; 
	private Collection<Class> allTypes; 
	private String guid; 
	
	public ServiceRegistrationEntry(EndPoint endPoint, Class[] types, Object service) { 
		this.endPoint = endPoint; 
		this.types = types; 
		this.service = service; 
		allTypes = TypeUtil.getAllTypes(types); 
		guid = GUIDGenerator.createGUID(this); 
	}
	
	public EndPoint getEndPoint() { 
		return endPoint; 
	}
	
	public Class[] getTypes() { 
		return types; 
	}
	
	public Collection<Class> getAllTypes() { 
		return allTypes; 
	}
	
	public Object getService() { 
		return service; 
	}
	
	public String getGUID() { 
		return guid; 
	}
	
	public boolean match(Class[] targetTypes) { 
		for(int i=0; i<targetTypes.length; i++) { 
			if (! allTypes.contains(targetTypes[i])) { 
				return false; 
			}
		}
		return true; 
	}
	
	public int hashCode() { 
		return service.hashCode() ^ endPoint.hashCode(); 
	}
	
	public boolean equals(Object another) { 
		if (another instanceof ServiceRegistrationEntry) { 
			ServiceRegistrationEntry entry = (ServiceRegistrationEntry)another; 
			return this.service.equals(entry.getService()) && this.endPoint.equals(entry.getEndPoint()); 
		}
		return false; 
	}
}
